import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:puppy_store/models/order_payment_model.dart';
import 'package:puppy_store/pages/home/widgets/loadingAnimation.dart';
import 'package:puppy_store/pages/order_payment/payment_failed_page.dart';
import 'package:puppy_store/pages/order_payment/payment_success_page.dart';
import 'package:puppy_store/pages/order_payment/widget/order_list_bottom_bar.dart';
import 'package:puppy_store/service/order_payment.dart';
import 'package:puppy_store/widgets/custom_image.dart';
import 'package:puppy_store/widgets/custom_refresher.dart';
import 'package:puppy_store/widgets/price_widget.dart';
import 'package:flutter/material.dart';
import 'package:tobias/tobias.dart';

class OrderListContentWidget extends StatefulWidget {
  const OrderListContentWidget({this.orderState = 0, super.key});
  // 订单状态
  final int? orderState;

  @override
  _OrderListContentWidgetState createState() => _OrderListContentWidgetState();
}

class _OrderListContentWidgetState extends State<OrderListContentWidget>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  // 订单列表总数据模型
  OrderListModel? _ordersListModel;

  // 订单列表数据
  List<OrderInfoModel> _items = [];

  // 刷新控制器
  late RefreshController _refreshController;

  // 记录当前页码
  int _currentPage = 1;

  /// 记录当前操作的订单模型
  OrderInfoModel? _currentInfoModel;

  @override
  void initState() {
    _refreshController = RefreshController();

    // 刷新订单列表数据
    _refreshOrderListData();
    super.initState();
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  // 支付宝支付
  void _orderAlipay() async {
    try {
      String orderString = await OrderPaymentAPI.orderAlipay(
        _currentInfoModel!.id!,
      );
      debugPrint('支付宝支付: $orderString');
      // 初始化 tobias
      Tobias tobias = Tobias();
      // 判断是否安装了支付宝
      bool isAlipayInstalled = await tobias.isAliPayInstalled;

      if (!isAlipayInstalled) {
        EasyLoading.showToast('请先安装支付宝');
        return;
      }
      // 打开支付宝沙箱版进行支付
      Map alipayRes = await tobias.pay(orderString, evn: AliPayEvn.sandbox);

      // 根据支付结果进行相应的处理
      if (alipayRes['resultStatus'] == '9000') {
        // 支付成功
        Navigator.pushReplacement(
          context,
          CupertinoPageRoute(
            builder: (BuildContext context) {
              return PaymentSuccessPage();
            },
          ),
        );
      } else {
        // 这里用户取消支付 支付失败,停留在当前页面不做任何操作
      }
      debugPrint('支付宝支付结果: $alipayRes');
    } on DioException catch (e) {
      EasyLoading.showToast(e.message ?? '支付宝支付失败');
    }
  }

  /*
  1：待付款：取消订单 + 继续支付
  2：待发货：再次购买
  3：待收货：再次购买 + 查看物流 + 确认收货
  4：待评价：再次购买 + 去评价
  5：已完成：再次购买 + 删除订单
  6：已取消/关闭：删除订单
  */
  /// 构建底部操作栏
  List<Widget> _buildBottomBar(OrderInfoModel orderInfoModel, int index) {
    List<Widget> bars = [];

    switch (orderInfoModel.orderState!) {
      case 1:
        bars = [
          OrderListBottomBar(
            title: '取消订单',
            onTap: () {
              _currentInfoModel = orderInfoModel;

              // 难道索引
              _buildCancelOrderDialog(index);
            },
          ),
          Padding(
            padding: EdgeInsets.only(left: 10.0),
            child: OrderListBottomBar(
              title: '继续支付',
              onTap: () {
                _currentInfoModel = orderInfoModel;
                // 重新唤起支付宝再次支付一次即可,如果出现取消订单和支付失败,什么都不用做,因为这两种情况没有修改数据
                _orderAlipay();
              },
            ),
          ),
        ];
        break;
      case 2:
        bars = [OrderListBottomBar(title: '再次购买', onTap: () {})];
        break;
      case 3:
        bars = [
          OrderListBottomBar(title: '再次购买', onTap: () {}),
          Padding(
            padding: EdgeInsets.only(left: 10.0),
            child: OrderListBottomBar(title: '查看物流', onTap: () {}),
          ),
          Padding(
            padding: EdgeInsets.only(left: 10.0),
            child: OrderListBottomBar(title: '确认收货', onTap: () {}),
          ),
        ];
        break;
      case 4:
        bars = [
          OrderListBottomBar(title: '再次购买', onTap: () {}),
          Padding(
            padding: EdgeInsets.only(left: 10.0),
            child: OrderListBottomBar(title: '去评价', onTap: () {}),
          ),
        ];
        break;
      case 5:
        bars = [
          OrderListBottomBar(title: '再次购买', onTap: () {}),
          Padding(
            padding: EdgeInsets.only(left: 10.0),
            child: OrderListBottomBar(title: '删除订单', onTap: () {}),
          ),
        ];
        break;
      case 6:
        bars = [OrderListBottomBar(title: '删除订单', onTap: () {})];
        break;
      default:
        break;
    }

    return bars;
  }

  /// 构建状态字符串
  /// 订单状态：1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消
  Widget _buildOrderStateText(int orderState) {
    String text = '';
    switch (orderState) {
      case 1:
        text = '待付款';
        break;
      case 2:
        text = '待发货';
        break;
      case 3:
        text = '待收货';
        break;
      case 4:
        text = '待评价';
        break;
      case 5:
        text = '已完成';
        break;
      case 6:
        text = '交易关闭';
        break;
      default:
        break;
    }

    Color color;
    if (orderState == 5 || orderState == 6) {
      color = Color(0xFF999999);
    } else {
      color = Color(0xFFFF9240);
    }

    return Text(text, style: TextStyle(color: color, fontSize: 14.0));
  }

  // 获取订单列表数据
  void _refreshOrderListData() async {
    // 重置页码
    _currentPage = 1;

    // 如果在下拉刷新的时候, 需要重置上拉加载的状态
    _refreshController.loadComplete();

    try {
      _ordersListModel = await OrderPaymentAPI.orderList(
        orderState: widget.orderState,
      );

      _items = _ordersListModel!.items!;

      _refreshController.refreshCompleted();
      setState(() {});
    } on DioException catch (e) {
      _refreshController.refreshFailed();
      debugPrint('接口调用异常: $e');
    }
  }

  /// 构建订单商品
  Widget _buildOrderSkus(List<OrderSkuModel> skuModels) {
    return ListView.builder(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      itemCount: skuModels.length,
      itemBuilder: (BuildContext context, int index) {
        OrderSkuModel skuModel = skuModels[index];
        return Container(
          padding: EdgeInsets.symmetric(vertical: 10.0),
          color: Colors.white,
          child: Row(
            children: [
              // 图片
              CustomImage(url: skuModel.image!, width: 86.0, height: 86.0),
              // 商品名称+数量+规格+价格
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(left: 10.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 名称+数量
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Text(
                              skuModel.name!,
                              style: TextStyle(
                                color: Color(0xFF262626),
                                fontSize: 13.0,
                              ),
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 15.0),
                            child: Text(
                              'x ${skuModel.quantity!}',
                              style: TextStyle(
                                fontSize: 14.0,
                                color: Color(0xFF262626),
                              ),
                            ),
                          ),
                        ],
                      ),
                      // 规格
                      Padding(
                        padding: EdgeInsets.only(top: 6.0),
                        child: Container(
                          padding: EdgeInsets.symmetric(
                            vertical: 2.0,
                            horizontal: 6.0,
                          ),
                          decoration: BoxDecoration(
                            color: Color(0xFFF7F7F8),
                            borderRadius: BorderRadius.circular(2.0),
                          ),
                          child: Text(
                            skuModel.attrsText!,
                            style: TextStyle(
                              color: Color(0xFF888888),
                              fontSize: 11.0,
                              height: 1.0,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                      // 价格
                      Padding(
                        padding: EdgeInsets.only(top: 6.0),
                        child: PriceWidget(price: skuModel.curPrice!),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  /// 构建订单列表元素
  Widget _buildOrderItem(int index) {
    OrderInfoModel infoModel = _items[index];
    return Container(
      margin: EdgeInsets.only(top: 10.0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5.0),
        ),
        clipBehavior: Clip.antiAlias,
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: [
            // 订单时间 + 状态
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // 订单时间
                Text(
                  infoModel.createTime!,
                  style: TextStyle(color: Color(0xFF666666), fontSize: 13.0),
                ),
                // 订单状态
                _buildOrderStateText(infoModel.orderState!),
              ],
            ),
            // 订单商品信息
            Padding(
              padding: EdgeInsets.only(top: 10.0),
              child: _buildOrderSkus(infoModel.skus!),
            ),
            // 订单实付款
            Padding(
              padding: EdgeInsets.only(top: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    '合计：',
                    style: TextStyle(color: Color(0xFF262626), fontSize: 12.0),
                  ),
                  PriceWidget(
                    price: infoModel.payMoney!,
                    symbolFontSize: 12.0,
                    integerFontSize: 16.0,
                    decimalFontSize: 12.0,
                  ),
                ],
              ),
            ),
            // 订单状态对应的选项：不同的订单状态对应不同的选项
            Padding(
              padding: EdgeInsets.only(top: 15.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: _buildBottomBar(infoModel, index),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建空订单列表
  Widget _buildEmptyOrderList() {
    return GestureDetector(
      onTap: () {
        _refreshOrderListData();
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset('assets/empty_content.png'),
          Text(
            "暂无订单信息哦!",
            style: TextStyle(fontSize: 16.0, color: Color(0xFF666666)),
          ),
        ],
      ),
    );
  }

  // 上拉加载更多
  void _loadMore() async {
    // 页码累加
    _currentPage++;

    // 如果上拉加载的时候,更新下拉刷新的状态
    _refreshController.refreshCompleted();

    try {
      _ordersListModel = await OrderPaymentAPI.orderList(
        page: _currentPage,
        orderState: widget.orderState,
      );

      _items.addAll(_ordersListModel!.items!);

      // 如果加载的数据小于每页总数,说明已经到了最后一页

      if (_ordersListModel!.items!.length < 10) {
        _refreshController.loadNoData();
      } else {
        _refreshController.loadComplete();
      }

      setState(() {});
    } on DioException catch (e) {
      _refreshController.loadFailed();
      EasyLoading.showToast('接口调用异常: $e');
    }
  }

  // 取消订单
  void _orderCancel(int orderIndex) async {
    try {
      OrderInfoModel updateInfoModel = await OrderPaymentAPI.orderCancel(
        _currentInfoModel!.id!,
        '用户取消订单',
      );

      // 判断当前是在<全部>和<待付款>页面
      if (widget.orderState == 0) {
        // 重新设置订单列表的状态
        _items[orderIndex] = updateInfoModel;
      } else {
        // 直接移除订单
        _items.removeAt(orderIndex);
      }
      setState(() {});
      debugPrint('取消订单: $updateInfoModel');
    } on DioException catch (e) {
      EasyLoading.showToast('接口调用异常: $e');
    }
  }

  /// 构建删除对话框
  void _buildCancelOrderDialog(int orderIndex) {
    showGeneralDialog(
      context: context,
      barrierDismissible: true, // 点击背景是否可以关闭对话框
      barrierLabel: '',
      transitionDuration: Duration(milliseconds: 200),
      pageBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
      ) {
        return Center(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 30.0),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.0),
              color: Colors.white,
            ),
            clipBehavior: Clip.antiAlias,
            width: MediaQuery.of(context).size.width - 100.0,
            height: 138.0,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  '确认取消订单?',
                  style: TextStyle(color: Color(0xFF333333), fontSize: 16.0),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 28.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: GestureDetector(
                          child: Container(
                            height: 40.0,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(2.0),
                              border: Border.all(color: Color(0xFF666666)),
                            ),
                            child: Text(
                              '取消',
                              style: TextStyle(
                                fontSize: 13.0,
                                color: Color(0xFF666666),
                              ),
                            ),
                          ),
                          onTap: () {
                            Navigator.of(context, rootNavigator: true).pop();
                          },
                        ),
                      ),
                      SizedBox(width: 16.0),
                      Expanded(
                        child: GestureDetector(
                          onTap: () {
                            // 调用取消订单的接口方法
                            _orderCancel(orderIndex);
                            Navigator.of(context, rootNavigator: true).pop();
                          },
                          child: Container(
                            height: 40.0,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(2.0),
                              gradient: LinearGradient(
                                colors: [Color(0xff3CCEAF), Color(0xff27BA9B)],
                              ),
                            ),
                            child: Text(
                              '确认',
                              style: TextStyle(
                                fontSize: 13.0,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  // 展示订单列表内容
  Widget _buildContent() {
    return _items.length > 0
        ? Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: CustomRefresher(
            controller: _refreshController,
            enablePullDown: true,
            enablePullUp: true,
            onRefresh: _refreshOrderListData,
            onLoading: _loadMore,
            child: ListView.builder(
              itemCount: _items.length,
              itemBuilder: (BuildContext context, int index) {
                return _buildOrderItem(index);
              },
            ),
          ),
        )
        : _buildEmptyOrderList();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return _ordersListModel != null ? _buildContent() : LoadingAnimation();
  }
}
